﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using StateGrid.MDBservice;
using System.IO;
using iTextSharp.text.pdf;

namespace StateGrid.DLBK
{
    class DLBK_MDB : MDBservice.MDBservice
    {
        public override String id
        {
            get
            {
                return "dianlibaokan";
            }
        }

        private DLBK_DAO dlbkDAO = new DLBK_DAO();

        public override void clear()
        {
            base.clear();
        }

        public override String getTest()
        {
            IList<DLBK_FL> r = dlbkDAO.GetAllDLBK_FL();
            String ret = String.Empty;
            foreach (DLBK_FL i in r)
            {
                ret += i.ToString();
            }
            return ret;
        }

        public DLBK_MDB()
        {
            pmTableName = "DLBK_WF";
            formatPmTableName = "DLBK";
            title = "电力报刊";
        }

        public override void setColumnList()
        {
            this.ColumnList = new List<Column>();
            if (hasFormatTable)
            {
                this.ColumnList.Add(new Column("分类编码", OleDbType.WChar, 20));
                this.ColumnList.Add(new Column("标题", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("作者", OleDbType.WChar, 150));
                this.ColumnList.Add(new Column("日期", OleDbType.Date));
                this.ColumnList.Add(new Column("版号", OleDbType.SmallInt));
                this.ColumnList.Add(new Column("栏目", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("正文", OleDbType.WChar, 0));
            }
            else
            {
                //未支持
            }
        }

        protected override PM getPM(OleDbDataReader reader, out String err)
        {
            err = String.Empty;
            DLBK_PM pm = new DLBK_PM();
            if (hasFormatTable)
            {
                pm.flbm = reader["分类编码"].ToString().Trim();
                pm.bt = reader["标题"].ToString().Trim();
                pm.zz = reader["作者"].ToString().Trim();
                try
                {
                    if (String.Empty.Equals(reader["日期"].ToString().Trim()))
                    {
                        //err += pm.flbm + "-" + pm.nf + ":" + pm.bt + ":日期为空\n";
                        pm.rq = DateTime.Parse("1900/1/1");
                    }
                    else
                        pm.rq = DateTime.Parse(reader["日期"].ToString().Trim());
                }
                catch
                {
                    err += pm.flbm + ":" + pm.bt + ":" + pm.zz +  ": 日期\"" + reader["日期"] + "\"出错\n";
                }
                try
                {
                    pm.bh = int.Parse(reader["版号"].ToString().Trim());
                }
                catch (Exception)
                {
                    pm.bh = 0;
                }
                pm.lm = reader["栏目"].ToString().Trim();
                pm.zw = reader["正文"].ToString().Trim();
            }
            else
            {
                //不支持
            }
            pm.dlr = user.name;
            pm.fillEmptyStringAsNull();
            return pm;
        }

        public override bool checkPMValid(PM pm, out string errMsg)
        {
            errMsg = String.Empty;
            DLBK_PM dlbk = (DLBK_PM)pm;
            this.openOleDbConnection();
            sqlcmd.CommandText = @"select * from " + formatPmTableName + " where 分类编码=@分类编码 and 标题=@标题 and 作者=@作者";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", dlbk.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("标题", dlbk.bt));
            sqlcmd.Parameters.Add(new OleDbParameter("作者", dlbk.zz));
            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return true;
            }
            else
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return false;
            }
            //            this.closeOleDbConnection();
        }

        public override string formatData(PM pm)
        {
            //反馈信息
            String result = String.Empty;
            String Empty = "NULL";
            DLBK_PM dlbk = (DLBK_PM)pm;

            dlbk.zz = GlobalMethod.Semicolon(dlbk.zz);
            dlbk.zz = dlbk.zz.Replace("本报记者", "");
            dlbk.zz = dlbk.zz.Replace("记者", "");
            dlbk.dlr = user.name;
            dlbk.gxrq = DateTime.Now;
            if (dlbkDAO.GetDLBK_FLByFlbm(dlbk.flbm) == null)
            {
                result += "分类编码" + dlbk.flbm + "不存在 ";
            }
            if (Empty.Equals(dlbk.bt))
            {
                result += "标题为空 ";
            }

            if (!result.Equals(String.Empty))
            {
                result += " : " + dlbk.zz + "《" + dlbk.bt + "》\n";
            }

            return result;
        }

        public override String insertPM(PM pm)
        {
            DLBK_PM dlbk = (DLBK_PM)pm;
            int back;
            String err = String.Empty;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(20), 标题 char(255), 作者 char(150), 日期 datetime, 版号 smallint, 栏目 char(100), 正文 text)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return err;
                }
            }

            sqlcmd.CommandText = @"insert into " + formatPmTableName + @"(分类编码,标题,作者,日期,版号,栏目,正文) values(@分类编码,@标题,@作者,@日期,@版号,@栏目,@正文)";
            sqlcmd.Connection = this.ObjConnection;
            OleDbParameter parameter;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", dlbk.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("标题", dlbk.bt));
            sqlcmd.Parameters.Add(new OleDbParameter("作者", dlbk.zz));
            parameter = new OleDbParameter("日期", OleDbType.DBDate);
            parameter.Value = dlbk.rq;
            sqlcmd.Parameters.Add(parameter);
            sqlcmd.Parameters.Add(new OleDbParameter("版号", dlbk.bh));
            sqlcmd.Parameters.Add(new OleDbParameter("栏目", dlbk.lm));
            sqlcmd.Parameters.Add(new OleDbParameter("正文", dlbk.zw));
            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();

            this.closeOleDbConnection();
            return err;
        }

        public override int deletePM(PM pm, out String err)
        {
            int back = 0;
            err = String.Empty;
            DLBK_PM dlbk = (DLBK_PM)pm;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(20), 标题 char(255), 作者 char(150), 日期 datetime, 版号 smallint, 栏目 char(100), 正文 text)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return 0;
                }
            }
            //sql语句
            sqlcmd.CommandText = @"delete from " + formatPmTableName + " where 分类编码=@分类编码 and 标题=@标题 and 作者=@作者";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", dlbk.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("标题", dlbk.bt));
            sqlcmd.Parameters.Add(new OleDbParameter("作者", dlbk.zz));

            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = "Exception: " + e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();
            //            this.closeOleDbConnection();
            return back;
        }
        /* 现在的方法先delete再insert，就不用为每文库写update了
        public override bool updatePM(PM pm)
        {
            ZWQK_PM zwqk = (ZWQK_PM)pm;
            //打开连接
            this.openOleDbConnection();

            //sql语句
            OleDbCommand sqlcmd = new OleDbCommand(@"update ZWQK set 作者=@作者,作者单位=@作者单位,中文主题词=@中文主题词,外文主题词=@外文主题词,国图分类=@国图分类 where 中文篇名=@中文篇名 and 文件名=@文件名", this.ObjConnection);

            sqlcmd.Parameters.Add(new OleDbParameter("作者", zwqk.zz));
            sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwqk.zzdw));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwqk.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwqk.wwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwqk.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwqk.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwqk.wjm));
            int back = sqlcmd.ExecuteNonQuery();
            if (back == 0)
            {
                return false;
            }
            //objConnection.Close();
            return true;
        }*/

        public override bool findServerDuplicate(PM pm)
        {
            DLBK_PM dlbk = (DLBK_PM)pm;
            IList<DLBK_PM> result = dlbkDAO.QueryDLBK_PM(dlbk.flbm, dlbk.bt, dlbk.zz);
            if (result != null && result.Count != 0)
                return true;
            else
                return false;
        }

        public override String insertPMwithUpdateWJM(PM pm)
        {
            String err = String.Empty;
            DLBK_PM dlbk = (DLBK_PM)pm;
            try
            {
                dlbkDAO.InsertDLBK_PM(dlbk);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

        public override string addOneRecord(PM pm)
        {
            String err = String.Empty;
            DLBK_PM dlbk = (DLBK_PM)pm;
            dlbk.gxrq = DateTime.Now;
            dlbk.eraseNullInEmptyString();
            try
            {
                dlbkDAO.InsertDLBK_PM(dlbk);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

    }
}
